package com.www.applyword.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.allen.library.SuperButton;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.bean.SettlementBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.home.CommentActivity;
import com.www.applyword.ui.home.SettlementActivity;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.Utils;

import java.util.HashMap;
import java.util.Map;

/**
 * author : huang
 * date   : 2019/10/21 0021 15:06
 * version: 1.0
 * @author Administrator
 */
public class HomeAdapter4 extends CommonRecyclerAdapter<SettlementBean> {

    private EditText view;
    private SettlementActivity context;


    public HomeAdapter4(Context context) {
        super(context, R.layout.item_list_settlement);
        this.context = (SettlementActivity) context;
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, SettlementBean item, int position) {
        helper.setText(R.id.tv_my_name,item.getOrderItem_UserName());
        helper.setText(R.id.tv_money,"￥" + item.getOrderItem_Money());
        ImageUtil.setImage(helper.getView(R.id.tv_my_photo), (String) item.getOrderItem_UserHeadImg());
        helper.setText(R.id.et_money,"￥" +  item.getOrderItem_Bond() + "");
        view = helper.getView(R.id.et_money);
        view.setSelection(view.getText().toString().length());
        SuperButton superButton = helper.getView(R.id.sb3);

        if (item.getOrderItem_State() == 5){
            helper.getView(R.id.sb1).setVisibility(View.INVISIBLE);
            superButton.setText("确认异常结算");
        }else if (item.getOrderItem_State() == 4){
            superButton.setText("已异常结算");
            helper.getView(R.id.sb3).setEnabled(false);
        }

        helper.getView(R.id.sb1).setOnClickListener(v -> {
            Intent intent = new Intent(context, CommentActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("BusinessID", "");
            intent.putExtra("OrderItemsID", item.getOrderItem_ID());
            // intent.putExtra("identifier", identifier);
            Utils.getContext().startActivity(intent);
        });
        //正常结算
        helper.getView(R.id.sb2).setOnClickListener(v -> {
            Map<String, Object> params = new HashMap<>();
            params.put("OrderItemsID",item.getOrderItem_ID());
            HttpClient.get(context, Constants.SETTLEMENT, params,new CallBack<LoginBean>() {
                @Override
                public void onSuccess(LoginBean result) {
                     notifyDataSetChanged();
                }
            });
        });
        //异常结算
        helper.getView(R.id.sb3).setOnClickListener(v -> {
            if (superButton.getText().toString().equals("异常结算")){
                if (TextUtils.isEmpty(view.getText().toString().trim())){
                    Utils.showToastSafe("请输入金额");
                    return;
                }
                Map<String, Object> params = new HashMap<>();
                params.put("OrderItemsID",item.getOrderItem_ID());
                params.put("Money",view.getText().toString().trim().substring(1,view.getText().toString().length()));
                HttpClient.get(context, Constants.ABNORMALSETTLEMENT, params,new CallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean result) {
                        notifyDataSetChanged();
                    }
                });
            }else {
                Map<String, Object> params = new HashMap<>();
                params.put("OrderItemsID",item.getOrderItem_ID());
                params.put("Money",view.getText().toString().trim().substring(1,view.getText().toString().length()));
                HttpClient.get(context, Constants.ABNORMALSETTLEMENTAPI, params,new CallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean result) {
                        notifyDataSetChanged();
                    }
                });
            }
        });
    }
}

